<?php
// +----------------------------------------------------------------------
// | OneKeyAdmin [ Believe that you can do better ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020-2023 http://onekeyadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: MUKE <513038996@qq.com>
// +----------------------------------------------------------------------
namespace plugins\userinvite\api\listen;

use app\api\model\User;
use app\api\model\UserGroup;
use app\api\model\Config;
use plugins\userinvite\api\model\UserInvite;

class RegisterEnd
{
    public function handle($registerInfo)
    {
        $input = input('post.');
        if (! empty($input['uid'])) {
            $uid    = $input['uid'];
            $config = Config::getVal('app_userinvite');
            if (! $config) {
                $config = ['open' => 0];
            }
            if ($config['open'] == 1) {
                $integral = $config['integral'];  
                // 修改邀请人信息
                $user = User::where('id', $uid)->find();
                $user->history_integral = $user->history_integral + $integral;
                $user->now_integral     = $user->now_integral + $integral;
                $user->save();
                // 插入日志记录
                UserInvite::create([
                    'my'          => $uid,
                    'to'          => $registerInfo->id,
                    'integral'    => $integral,
                    'create_time' => date('Y-m-d H:i:s'),
                ]);
            }
        }
    }   
}